
//--------------------------------------------------------------------------------------------------------------------
// register the function
 var newRegistery1 = new Array("getComments", "displayComments" );
 allJsonsRegistry = allJsonsRegistry.concat(newRegistery1);


//--------------------------------------------------------------------------------------------------------------------
// implement the function
// fills the table displaying the comments
function displayComments(commentTable) 
{
    // access the comment section
    my_comments = document.getElementById('id_display_comments_section');
    setStyleAttribute( my_comments, 'overflow:scroll;' );
    
    //clean the comments
    if( my_comments.hasChildNodes() ) 
    {
        while ( my_comments.childNodes.length >= 1 )
        {
            my_comments.removeChild( my_comments.firstChild );       
        }
     }

    /// add the new comments. Each comment is a comment box wich contains one or 2 boxes.
    /// if we own the comment: a box with the text and a "+" button to hide the comment
    /// if we do not own it: just the text
    for( var i = 0; i <  commentTable.comments.length; ++i )
    {
        // create the div, one over two will be grey
        var newdiv = document.createElement('div');
        var divIdName = 'myCommentDiv'+i;
        setClassAttribute(newdiv,'commentContainer');
        if( ( 2 * Math.floor(i/2) ) == i )
        {
            setStyleAttribute(newdiv,'background: #F5F5F5;');
        }
        else
        {
            setStyleAttribute(newdiv,'background: white;');
        } 

        // add the text
        var newdivTxt = document.createElement('div');
        var divIdNameTxt = 'myCommentDivTxt'+i;
        newdivTxt.setAttribute('id',divIdNameTxt);
        setClassAttribute(newdivTxt,'commentText');
        setStyleAttribute(newdivTxt,'background: inherit;');
        var str = commentTable.comments[i].postTime+ ', ' + commentTable.comments[i].posterName+ " says: <br/>";
        newdivTxt.innerHTML = str + commentTable.comments[i].textValue + '<br/>';
        newdiv.appendChild(newdivTxt);

        // add the "+" boutton that allow to hide the comment when we are the owner.
        // only the owner of the comment is allowed to hide a comment
        if( commentTable.comments[i].belongToPlayer )
        {
            var newdivDelete = document.createElement('div');
            var divIdNameDelete = 'myCommentDivDelete'+i;
            newdivDelete.setAttribute('id',divIdNameDelete);
            setClassAttribute(newdivDelete,'commentDelete');
            newdivDelete.innerHTML = "<form action=\"/hideComment/\" method=\"post\" onclick=\"hideComment("+commentTable.comments[i].idNumber+")\" >&#215;</form>";
            newdiv.appendChild(newdivDelete);
        }
        
        // add the div at the end 
        my_comments.appendChild(newdiv);
    }
    
    // put the scroll bar at the end
    my_comments = document.getElementById('id_display_comments_section');   
    my_comments.scrollTop = my_comments.scrollHeight;

    //set the input to ""
    my_comment = document.getElementById('id_comment_text');
    my_comment.value = "";
    return false;
};

//--------------------------------------------------------------------------------------------------------------------
// extract the object and call the mainb  function
function displayCommentsWithPythonReturnEval(commentTable)
{
    commentTableObj = eval( commentTable.responseText );
    displayComments(commentTableObj[0]);
    return false;
} 

//--------------------------------------------------------------------------------------------------------------------
// suppress the commment with id idNumber
function hideComment(idNumber)
{
    var args = { 
           type: "POST", 
           url: "/hideComment/", 
           cache: false, 
           data: { 'idNumber' : idNumber }, 
           complete:displayCommentsWithPythonReturnEval,
           error: function(errorThrown)  { document.write(errorThrown.responseText);  }
           };
    $.ajax(args);
    return false;    
}


//--------------------------------------------------------------------------------------------------------------------
// post the comment which is written in the text area id_comment_text 
function postComment()
{
    my_comment = document.getElementById('id_comment_text');
    var args = { 
           type: "POST", 
           url: "/postComment/", 
           cache: false, 
           data: { 'commentText' : my_comment.value }, 
           complete:displayCommentsWithPythonReturnEval,
           error: function(errorThrown)  { document.write(errorThrown.responseText);  }
           };
    $.ajax(args);
}